﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace background.Manager.Action {
    /// <summary>
    /// fromData 的摘要说明
    /// </summary>
    public class fromData: IHttpHandler, IRequiresSessionState {

        public void ProcessRequest(HttpContext context) {
            context.Response.ContentType = "text/plain";
            System.Text.StringBuilder ret=new System.Text.StringBuilder();
            switch(context.Request["action"].ToString()) {
                case "login":
                    System.Data.DataTable dt= ManagerConfig.login(context.Request["loginName"], md5(context.Request["password"]));
                    if(dt.Rows.Count==0) {
                        ret.Append("-1");
                    } else {
                        ret.Append("1");
                        context.Session[ManagerConfig.bg("sessionName")]=dt.Rows[0]["ID"];
                        context.Session["nickname"]=dt.Rows[0]["NickName"];
                        context.Session["loginname"]=dt.Rows[0]["loginName"];
                    }
                    break;
                case "createUser":
                    if(ManagerConfig.existsLoginName(context.Request["loginname"])) {
                        ret.Append("2"); context.Response.Write(ret); return;
                    }
                    ret.Append(ManagerConfig.createManagerUser(context.Request["nickname"], context.Request["loginname"], md5(context.Request["password"])).ToString().ToLower());
                    break;
                case "updatePwd":
                    ret.Append(ManagerConfig.updateUserPassword(context.Request["id"], md5(context.Request["password"])).ToString().ToLower());
                    break;
                case "updateInfo":
                    ret.Append(ManagerConfig.updateUserInfo(context.Request["id"], context.Request["nickname"]).ToString().ToLower());
                    break;
                case "delete":
                    ret.Append(ManagerConfig.massDeleteUser(context.Request["idList"]).ToString().ToLower());
                    break;
            }

            context.Response.Write(ret);
        }

        string md5(string content) {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(content, "MD5");
        }

        public bool IsReusable {
            get {
                return false;
            }
        }
    }
}